package com.fdb.efp.esb.service.impl.frontsystem;

import com.fdb.efp.esb.common.client.resp.EsbRespRetInfBean;
import com.fdb.efp.esb.common.client.resp.EsbRespServiceBean;
import com.fdb.efp.esb.common.client.resp.EsbRespSysHeadBean;
import com.fdb.efp.esb.service.facade.frontsystem.AccountingPageinfoService;
import com.fdb.basic.framework.core.constant.ESBConstant;
import com.fdb.basic.framework.core.exception.ESBException;
import com.fdb.efp.esb.service.bo.req.frontsystem.ReqAccountingPageinfoBean;
import com.fdb.efp.esb.service.bo.resp.frontsystem.RespAccountingPageinfoBean;
import com.fdb.efp.esb.service.client.ESBClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 *需求编号：【】（业务提的需求编号）
 *问题编号：【】（若不是因为修复问题而新增该类，则留空）
 *开发人员：【】
 *创建日期：【2019-11-2019/11/5 9:21】
 *功能描述：调用互金提供核销接口实现类
 */
@Service("accountingPageinfoService")
public class AccountingPageinfoServiceImpl implements AccountingPageinfoService {

    private final Logger logger = LoggerFactory.getLogger(AccountingPageinfoService.class);

    @Override
    public RespAccountingPageinfoBean getAccountingPageinfo(ReqAccountingPageinfoBean reqAccountingPageinfoBean) throws ESBException {
        // 发起请求
        EsbRespServiceBean esbRespServiceBean = ESBClientFactory.buildClient().withTradeNo("").withScene("").withBody(reqAccountingPageinfoBean).create().sendAndReceive();
        // 返回对象
        RespAccountingPageinfoBean respAccountingPageinfoBean = null;
        // 判断是否成功
        Optional<EsbRespSysHeadBean> sysHead = Optional.ofNullable(esbRespServiceBean.getSysHead());
        if (sysHead.isPresent()) {
            Optional<EsbRespRetInfBean> esbRespRetInfBean = sysHead.get().getRetInfArry().stream().findFirst();
            if (esbRespRetInfBean.isPresent()) {
                String retCd = esbRespRetInfBean.get().getRetCd();
                String retMsg = esbRespRetInfBean.get().getRetMsg();
                if (ESBConstant.SUCCESS.equals(retCd)) {
                    String msg = "调用互金提供核销接口成功";
                    logger.debug(msg);
                    respAccountingPageinfoBean = esbRespServiceBean.getBody(RespAccountingPageinfoBean.class);
                } else {
                    String msg = "调用互金提供核销接口失败" + retMsg;
                    logger.error(msg);
                    throw new ESBException(msg);
                }
            }
        }
        return respAccountingPageinfoBean;
    }
}
